'''
Created on 2010-3-2

@author: Administrator
'''
from jolly.datastructures.Queue import Queue
from jolly.datastructures.Deque import Deque
from jolly.datastructures.QueueAsArray import QueueAsArray
from jolly.datastructures.Exception import *

class DequeAsArray(QueueAsArray, Deque):
    '''
    Deque implemented using an array
    '''


    def __init__(self, size=0):
        '''
        (DequeArray, int)->None
        Constructs a deque of the given size.
        '''
        super(DequeAsArray, self).__init__(size)
        
    def enqueueHead(self, obj):
        '''
        (DequeAsArray, Object)->None
        Enqueues the given object at the head of this queue.
        '''
        if self._count == len(self._array):
            raise ContainerFull
        if self._head == 0:
            self._head = len(self._array)-1
        else:
            self._head = self._head-1
        self._array[self._head]=obj
        self._count+=1
    
    def getTail(self):
        '''
        (DequeAsArray)->Obj
        Returns the object at the tail of this deque
        '''
        if self._count == 0:
            raise ContainerEmpty
        return self._array[self._tail]
    
    def dequeueTail(self):
        '''
        (DequeAsArray)->Object
        Dequeues the object at the tail of this deque. 
        '''
        if self._count == 0:
            raise ContainerEmpty
        result = self._array[self._tail]
        self._array[self._tail]=None
        if self._tail==0:
            self._tail = len(self._array)-1
        else:
            self._tail = self._tail-1
        self._count -= 1
        return result
    
    @staticmethod
    def main(*argv):
        "DequeAsArray test program."
        print DequeAsArray.main.__doc__
        deque1 = DequeAsArray(5)
        Queue.test(deque1)
        Deque.test(deque1)
        return 0
    
if __name__ == "__main__":
    sys.exit(DequeAsArray.main())